//
//  AttractionsListService.m
//  HZHome
//
//  Created by SunShine on 13-10-13.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "AttractionsListService.h"
#import "AttractionItem.h"
#import "NSObject+propertyList.h"

@interface AttractionsListService()
-(void)initDataListwithArray:(NSArray*)array;
@end

@implementation AttractionsListService

-(void)requestList
{
    MKNetworkEngine *engine = [[MKNetworkEngine alloc]initWithHostName:ATTRACTIONSLIST customHeaderFields:nil];
    MKNetworkOperation *op = [engine operationWithPath:@"" params:nil];
    [op addCompletionHandler:^(MKNetworkOperation *operation){
        
        NSDictionary *json = [operation responseJSON];
        [self initDataListwithArray:[json objectForKey:@"pageList"]];
        //执行回调
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFinished)]) {
            [self.delegate loadDataFinished];
        }
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err){
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFaild:)]) {
            [self.delegate loadDataFaild:@"获取数据失败."];
        }
    }];
    [engine enqueueOperation:op];
}

-(void)initDataListwithArray:(NSArray*)array
{
    NSMutableArray *tempArray = [NSMutableArray array];
    if (self.dataList && self.dataList.count>0) {
        [tempArray addObjectsFromArray:self.dataList];
    }
    for (NSDictionary *dic in array) {
        AttractionItem *item = [[AttractionItem alloc]init];
        NSArray *properties = [item getPropertyList];
        for (NSString *propertyName in properties) {
            NSString *key = propertyName;
            if ([propertyName isEqualToString:@"Id"]) {
                key = @"id";//因为属性不能叫做id所以把I变成了大写
            }
            [item setValue:[dic objectForKey:key] forKey:propertyName];
        }
        [tempArray addObject:item];
    }
    self.dataList = tempArray;
}
@end
